package com.wanli.agent.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 朱永强 2020/9/1/001
 */
public final class StringUtils {

    private StringUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * Google Gson
     *
     * @param jsonInString
     * @return
     */
    public final static boolean isJSON(String jsonInString) {
        try {
            new Gson().fromJson(jsonInString, Object.class);
            return true;
        } catch (JsonSyntaxException ex) {
            return false;
        }
    }

    /**
     * null
     */
    public static String excessOmit(String string, int number) {


        if (string.length() >= number) {

            String charSequence = (String) string.subSequence(0, number);
            String stringTest = charSequence + "...";
            return stringTest;
        } else {
            return string;
        }
    }

    /**
     * 判断字符串是否为null或长度为0
     *
     * @param s 待校验字符串
     * @return {@code true}: 空<br> {@code false}: 不为空
     */
    public static boolean isEmpty(final CharSequence s) {
        return s == null || s.length() == 0;
    }

    /**
     * 判断字符串是否为null或全为空格
     *
     * @param s 待校验字符串
     * @return {@code true}: null或全空格<br> {@code false}: 不为null且不全空格
     */
    public static boolean isTrimEmpty(final String s) {
        return (s == null || s.trim().length() == 0);
    }

    /**
     * 判断字符串是否为null或全为空白字符
     *
     * @param s 待校验字符串
     * @return {@code true}: null或全空白字符<br> {@code false}: 不为null且不全空白字符
     */
    public static boolean isSpace(final String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断两字符串是否相等
     *
     * @param a 待校验字符串a
     * @param b 待校验字符串b
     * @return {@code true}: 相等<br>{@code false}: 不相等
     */
    public static boolean equals(final CharSequence a, final CharSequence b) {
        if (a == b) return true;
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }

    /**
     * 判断两字符串忽略大小写是否相等
     *
     * @param a 待校验字符串a
     * @param b 待校验字符串b
     * @return {@code true}: 相等<br>{@code false}: 不相等
     */
    public static boolean equalsIgnoreCase(final String a, final String b) {
        return a == null ? b == null : a.equalsIgnoreCase(b);
    }

    /**
     * null转为长度为0的字符串
     *
     * @param s 待转字符串
     * @return s为null转为长度为0字符串，否则不改变
     */
    public static String null2Length0(final String s) {
        return s == null ? "" : s;
    }

    /**
     * 返回字符串长度
     *
     * @param s 字符串
     * @return null返回0，其他返回自身长度
     */
    public static int length(final CharSequence s) {
        return s == null ? 0 : s.length();
    }

    /**
     * 首字母大写
     *
     * @param s 待转字符串
     * @return 首字母大写字符串
     */
    public static String upperFirstLetter(final String s) {
        if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) return s;
        return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1);
    }

    /**
     * 首字母小写
     *
     * @param s 待转字符串
     * @return 首字母小写字符串
     */
    public static String lowerFirstLetter(final String s) {
        if (isEmpty(s) || !Character.isUpperCase(s.charAt(0))) return s;
        return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1);
    }

    /**
     * 反转字符串
     *
     * @param s 待反转字符串
     * @return 反转字符串
     */
    public static String reverse(final String s) {
        int len = length(s);
        if (len <= 1) return s;
        int mid = len >> 1;
        char[] chars = s.toCharArray();
        char c;
        for (int i = 0; i < mid; ++i) {
            c = chars[i];
            chars[i] = chars[len - i - 1];
            chars[len - i - 1] = c;
        }
        return new String(chars);
    }

    /**
     * 转化为半角字符
     *
     * @param s 待转字符串
     * @return 半角字符串
     */
    public static String toDBC(final String s) {
        if (isEmpty(s)) return s;
        char[] chars = s.toCharArray();
        for (int i = 0, len = chars.length; i < len; i++) {
            if (chars[i] == 12288) {
                chars[i] = ' ';
            } else if (65281 <= chars[i] && chars[i] <= 65374) {
                chars[i] = (char) (chars[i] - 65248);
            } else {
                chars[i] = chars[i];
            }
        }
        return new String(chars);
    }

    /**
     * 转化为全角字符
     *
     * @param s 待转字符串
     * @return 全角字符串
     */
    public static String toSBC(final String s) {
        if (isEmpty(s)) return s;
        char[] chars = s.toCharArray();
        for (int i = 0, len = chars.length; i < len; i++) {
            if (chars[i] == ' ') {
                chars[i] = (char) 12288;
            } else if (33 <= chars[i] && chars[i] <= 126) {
                chars[i] = (char) (chars[i] + 65248);
            } else {
                chars[i] = chars[i];
            }
        }
        return new String(chars);
    }

    /**
     * 手机号码，中间4位星号替换
     *
     * @param phone 手机号
     * @return 星号替换的手机号
     */
    public static String phoneNoHide(String phone) {
        // 括号表示组，被替换的部分$n表示第n组的内容
        // 正则表达式中，替换字符串，括号的意思是分组，在replace()方法中，
        // 参数二中可以使用$n(n为数字)来依次引用模式串中用括号定义的字串。
        // "(\d{3})\d{4}(\d{4})", "$1****$2"的这个意思就是用括号，
        // 分为(前3个数字)中间4个数字(最后4个数字)替换为(第一组数值，保持不变$1)(中间为*)(第二组数值，保持不变$2)
        if ("null".equals(phone) || null == phone || "".equals(phone)) {
            return "";
        } else {
            return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }
    }


    /**
     * 手机号码
     */
    public static String spaceNoHide(String phone) {
        String tel = "";
        char[] chars = phone.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i == 3) {
                tel += " " + chars[i];
            } else if (i == 7) {
                tel += " " + chars[i];
            } else {
                tel += chars[i];
            }
        }

        return tel;
    }

    /**
     * 判断密码是否是字母加数字组合
     */
    public static Boolean verifyPassword(String password) {
        String regex = "^(?!([a-zA-Z]+|\\d+)$)[a-zA-Z\\d]{6,18}$";
        boolean matches = password.matches(regex);
        return matches;
    }

    /**
     *
     */
    public static String textNumber(int position) {

        if (position < 10) {
            return "0" + position + ".";
        } else {
            return position + ".";
        }

    }

    /**
     *
     */
    public static String isNumber(String str) {

        if ("0".equals(str) || "0.00".equals(str)) {
            return "";
        } else {
            return str;
        }

    }

    /*
     * 获取UID
     * */

    public static String getUUID() {
        String uuidStr = UUID.randomUUID().toString();
        uuidStr = uuidStr.substring(0, 8) + uuidStr.substring(9, 13) + uuidStr.substring(14, 18) + uuidStr.substring(19, 23) + uuidStr.substring(24);
        return uuidStr;
    }

    /**
     * 获取手机型号
     *
     * @return 手机型号
     */
    public static String getSystemModel() {
        return Build.MODEL;
    }

    /**
     * 返回当前程序版本号
     */
    public static String getAppVersionCode(Context context) {
        int versioncode = 0;
        String versionName = "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);

            versioncode = pi.versionCode;
            versionName = pi.versionName;

        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return "versioncode = " + versioncode + "versionName = " + versionName;
    }

    /**
     * 字符串为null时返回空字符串""
     */
    public static String nullString(String string) {

        if (null == string || "null".equals(string) || "".equals(string) || "nullnullnull".equals(string)) {
            return "";
        } else {
            return string;
        }
    }
    /**
     * 字符串为null时返回空字符串""
     */
    public static String nullStringZrg(String string) {

        if (null == string || "null".equals(string) || "".equals(string) || "nullnullnull".equals(string)) {
            return "转人工";
        } else {
            return string;
        }
    }
    public static String hindBank(String str) {

        String regex = "(lww{4})(.*)(1iw{4})";

        Matcher m = Pattern.compile(regex).matcher(str);

        if (m.find()) {
            String rep = m.group(2);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < rep.length(); i++) {
                sb.append("*");
            }
            return str.replaceAll(rep, sb.toString());
        } else {
            return str;
        }

    }

    /**
     * 字符串为null时返回空字符串""
     */
    public static String numberString(String string) {

        if (null == string || "null".equals(string) || "".equals(string) || "nullnullnull".equals(string)) {
            return "0";
        } else {
            return string;
        }
    }


    /**
     * 字符串为null时返回空字符串"无"
     */
    public static String nullStringNothing(String string) {

        if ("null".equals(string) || null == string || "".equals(string)) {
            return "无";
        } else {
            return string;
        }
    }

    /**
     * 字符串为null时返回空字符串""
     */
    public static String addstar(String string) {

        if ("null".equals(string) || null == string || "".equals(string)) {
            return "";
        } else {
            return string.substring(0, 1) + "**";
        }
    }

    /**
     * 字符串为null时返回空
     */
    public static String nullTheGinseng(String string) {

        if ("null".equals(string) || "".equals(string)) {
            return null;
        } else {
            return string;
        }
    }

    /**
     * 字符串为null时返回空
     */
    public static String isNullReturnNull(String string) {

        if (null == string) {
            return "";
        } else {
            return string;
        }
    }

    /**
     * 字符串为无时返回空
     */
    public static String isStringNull(String string) {

        if ("无".equals(string)) {
            return "";
        } else {
            return string;
        }
    }

    /**
     * encodeBase64File:(将文件转成base64 字符串). <br/>
     *
     * @param path 文件路径
     * @return
     * @throws Exception
     * @author guhaizhou@126.com
     * @since JDK 1.6
     */
    public static String encodeBase64File(String path) throws Exception {
        File file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return Base64.encodeToString(buffer, Base64.DEFAULT);
    }


    public static boolean ID_Card(String cardcode) {

        String cres = "^[0-9]*$";
        if (cardcode.matches(cres)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 身份证有限期转换
     */

    public static String conversionData(String invalid, String Issue) {
        String time = "";
        String invalidData = "";
        String issueData = "";
        char[] chars = invalid.toCharArray();
        char[] chars1 = Issue.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i == 3) {
                invalidData += chars[i] + ".";
            } else if (i == 5) {
                invalidData += chars[i] + ".";
            } else {
                invalidData += chars[i];
            }
        }

        for (int i = 0; i < chars1.length; i++) {
            if (i == 3) {
                issueData += chars1[i] + ".";
            } else if (i == 5) {
                issueData += chars1[i] + ".";
            } else {
                issueData += chars1[i];
            }
        }
        time = issueData + "-" + invalidData;
        return time;
    }


    /**
     * 身份证有限期转换
     */

    public static String conversionDataDate(String invalid) {
        String time = "";
        String invalidData = "";

        char[] chars = invalid.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            if (i == 3) {
                invalidData += chars[i] + "-";
            } else if (i == 5) {
                invalidData += chars[i] + "-";
            } else {
                invalidData += chars[i];
            }
        }


        time = invalidData;
        return time;
    }

    /**
     * 身份证号码，中间用星号替换
     *
     * @param cardNumber 身份证号码
     * @return 星号替换的身份证号码
     */
    public static String cardNoHide(String cardNumber) {
        // 括号表示组，被替换的部分$n表示第n组的内容
        // 正则表达式中，替换字符串，括号的意思是分组，在replace()方法中，
        // 参数二中可以使用$n(n为数字)来依次引用模式串中用括号定义的字串。
        // "(\d{3})\d{13}(\d{2})", "$1****$2"的这个意思就是用括号，
        // 分为(前3个数字)中间13个数字(最后2个数字)替换为(第一组数值，保持不变$1)(中间为*)(第二组数值，保持不变$2)
        if ("null".equals(cardNumber) || null == cardNumber || "".equals(cardNumber)) {
            return "";
        } else {
            return cardNumber.replaceAll("(\\d{3})\\d{13}(\\d{2})", "$1************$2");
        }
    }

    /**
     * 证件号码，中间用星号替换
     *
     * @param cardNumber 证件号码
     * @return 星号替换的证件号码
     */
    public static String IdentificationNumber(String cardNumber) {
        String number = "";
        if (cardNumber == null) return "";
        char[] chars = cardNumber.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i == 0) {
                number += chars[i];
            } else if (i == chars.length - 1) {
                number += chars[i];
            } else {
                number += "*";
            }
        }
        return number;
    }

    /**
     * 根据身份证号码第17位 奇数为  偶数为
     *
     * @param cardNumber 身份证号码
     */
    public static String cardSex(String cardNumber) {
        int number = Integer.parseInt(cardNumber);
        if (number % 2 == 0) {
            return "女";
        } else {
            return "男";
        }
    }

    /**
     * 获取Assets目录下的地址文件
     */
    public static String getAddress(String fileName, Context context) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


    // 删除ArrayList中重复元素，保持顺序
    public static List<String> removeDuplicateWithOrder(List<String> list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        list.clear();
        list.addAll(newList);
        return list;
    }

    /**
     * 集合转字符串
     */
    public static String listString(List<String> list) {
        String id = "";
        for (int i = 0; i < list.size(); i++) {
            if (i != list.size() - 1) {
                id += list.get(i) + ",";
            } else {
                id += list.get(i);
            }
        }

        return id;
    }

    /**
     * 百分比计算
     *
     * @param detailTotalNumber 当前数
     * @param totalNumber       总题数
     *                          占比=当前数/总题数
     * @return
     */
    public static Float percentInstance(Integer detailTotalNumber, Integer totalNumber) {
        Double bfTotalNumber = Double.valueOf(detailTotalNumber);
        Double zcTotalNumber = Double.valueOf(totalNumber);
        double percent = bfTotalNumber / zcTotalNumber;
        //获取格式化对象
        NumberFormat nt = NumberFormat.getPercentInstance();
        //设置百分数精确度2即保留两位小数
        nt.setMinimumFractionDigits(2);
        String format = nt.format(percent).split("%")[0];
        Float aFloat = Float.parseFloat(format);
        return aFloat;
    }

    /*
     * 时间转换
     * */
    public static String getHHMMSSDateDate(int date) {

        if (date < 60) {
            if (date < 10) {
                return "00:00:0" + date;
            } else {
                return "00:00:" + date;
            }

        } else if (date > 60 && date < 3600) {
            int m = date / 60;
            int s = date % 60;
            String mm = "";
            String ss = "";
            if (m < 10) {
                mm = "0";
            }
            if (s < 10) {
                ss = "0";
            }
            return "00:" + mm + m + ":" + ss + s;

        } else {
            int h = date / 3600;
            int m = (date % 3600) / 60;
            int s = (date % 3600) % 60;

            String hh = "";
            String mm = "";
            String ss = "";

            if (h < 10) {
                hh = "0";
            }
            if (m < 10) {
                mm = "0";
            }
            if (s < 10) {
                ss = "0";
            }
            return hh + h + ":" + mm + m + ":" + ss + s;
        }

    }

    /*
     * 时间转换
     * */
    public static String getMMSSDateDate(int date) {

        if (date < 60) {
            if (date < 10) {
                return "00:0" + date;
            } else {
                return "00:" + date;
            }

        } else if (date > 60 && date < 3600) {
            int m = date / 60;
            int s = date % 60;
            String mm = "";
            String ss = "";
            if (m < 10) {
                mm = "0";
            }
            if (s < 10) {
                ss = "0";
            }
            return mm + m + ":" + ss + s;

        } else if (date == 3600) {
            return "60:00";
        } else {
            int h = date / 3600;
            int m = (date % 3600) / 60;
            int s = (date % 3600) % 60;

            String hh = "";
            String mm = "";
            String ss = "";

            if (h < 10) {
                hh = "0";
            }
            if (m < 10) {
                mm = "0";
            }
            if (s < 10) {
                ss = "0";
            }
            return hh + h + ":" + mm + m + ":" + ss + s;
        }

    }

    /*
     * 获取当前时间
     * */

    public static String getCurrentTime() {
        //设置默认时区
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(day);
    }

    /**
     * 支持jdk1.7的写法<br/>
     * 解析2015-12-27T14:20:34+08:00格式类型的时间<br/>
     * 将2015-12-27T14:20:34+08:00转换成2015-12-27 14:20<br/>
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String getTimestampTimeV17(String str) throws ParseException {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        DateFormat df2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        df2.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date = null;
        try {
            date = df.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String value = df2.format(date);
        return value;
    }

    public static String getTimestampTimeV(String str) throws ParseException {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df2.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date = null;
        try {
            date = df.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String value = df2.format(date);
        return value;
    }

    /*
     * 集合转字符串
     * */
    public static String Sort(List<String> list) {

        if (list == null || list.size() == 0) {
            return "";
        }

        String content = "";
        for (int i = 0; i < list.size(); i++) {
            content += list.get(i);
        }
        return content;
    }

    /*
     * 集合转字符串
     * */
    public static String Sorts(List<String> list) {

        if (list == null || list.size() == 0) {
            return "";
        }

        String content = "";
        for (int i = 0; i < list.size(); i++) {
            if (i + 1 == list.size()) {
                content += list.get(i);
            } else {
                content += list.get(i) + ",";
            }
        }
        return content;
    }

    /**
     * 转换时间格式
     *
     * @param start 开始时间
     * @param end   结束时间
     */
    public static String ConvertTime(String start, String end) {

        String startTime = start.replace("年", ".").replace("月", ".").replace("日", "");
        String endTime = end.replace("年", ".").replace("月", ".").replace("日", "");

        return startTime + "-" + endTime;
    }

    /**
     * 验证手机格式
     */
    public static boolean isPhone(String number) {
        String charSequence = (String) number.subSequence(0, 1);
        int length = number.length();
        if (charSequence.equals("1") && length == 11) {
            return true;
        } else {
            return false;
        }
    }

    //系统剪贴板-获取:
    public static String getCopy(Context context) {


        // 获取系统剪贴板
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 返回数据
        ClipData clipData = clipboard.getPrimaryClip();
        if (clipData != null && clipData.getItemCount() > 0) {
            // 从数据集中获取（粘贴）第一条文本数据
            try {
                String s = clipData.getItemAt(0).getText().toString();
                return s;
            } catch (NullPointerException e) {
                return null;
            }

        }
        return null;
    }

    //系统剪贴板-复制:   s为内容
    public static void copy(Context context, String s) {
        // 获取系统剪贴板
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
        ClipData clipData = ClipData.newPlainText(null, s);
        // 把数据集设置（复制）到剪贴板
        clipboard.setPrimaryClip(clipData);
    }

    /**
     * 将字符串数组转换成集合list
     */
    public static List<String> StringListArray(String string) {
        List<String> list = new ArrayList<>();
        if (null == string || "".equals(string)) {
            list = new ArrayList<>();
        } else {
            String[] strs = string.split(",");
            for (int i = 0; i < strs.length; i++) {
                list.add(strs[i]);
            }
            //将字符串数组转换成集合list
        }
        return list;
    }

    /**
     * 将字符串数组转换成集合list
     */
    public static List<String> StringListArrays(String string) {
        List<String> list;
        if (null == string || "".equals(string)) {
            list = new ArrayList<>();
        } else {
            String[] strs = string.split(",");
            list = new ArrayList<>();
            for (int i = 0; i < strs.length; i++) {
                list.add(strs[i]);
            }
//            list = Arrays.asList(strs);
            //将字符串数组转换成集合list
        }
        return list;
    }
    /**
     * 将字符串数组转换成集合list
     */
    public static List<String> StringListArraysXieGang(String string) {
        List<String> list;
        if (null == string || "".equals(string)) {
            list = new ArrayList<>();
        } else {
            String[] strs = string.split("/");
            list = new ArrayList<>();
            for (int i = 0; i < strs.length; i++) {
                list.add(strs[i]);
            }
//            list = Arrays.asList(strs);
            //将字符串数组转换成集合list
        }
        return list;
    }
    /**
     * list取第一条
     */
    public static String StringListArray0(List<String> string) {

        if (string.size() == 0) {
            return "";
        } else {
            return string.get(0);
        }


    }

    /**
     * 将字符串数组转换成集合list
     */
    public static String StringArrList(ArrayList string) {
        String StringList = "";
        for (int i = 0; i < string.size(); i++) {
            StringList += string.get(i) + ",";
        }
        return StringList;
    }

    //对报单销货的操作时间处理
    public static String processingTime(String time) {

        String[] times = time.split(" ");

        String date = times[0];
        String time1 = times[1].substring(0, 5);

        int yeaterday = 1;
        try {
            yeaterday = isYeaterday(date, "yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (-1 == yeaterday) {
            return "今天" + " " + time1;
        } else if (0 == yeaterday) {
            return "昨天" + " " + time1;
        } else {
            String substring = date.substring(5, date.length());
            return substring + " " + time1;
        }
    }

    //判断日期是否是今天
    private static boolean isThisTime(String time, String pattern) {

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    /**
     * @param date 较小的时间
     * @param date 较大的时间 (如果为空   默认当前时间 ,表示和当前时间相比)
     * @return -1 ：同一天.    0：昨天 .   1 ：至少是前天.
     * @throws ParseException 转换异常
     * @author LuoB.
     */
    public static int isYeaterday(String date, String pattern) throws ParseException {
        Date newTime = new Date();
/*
* if(newTime==null){
			newTime=new Date();
		}
* */
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date oldTime = null;
        try {
            oldTime = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //将下面的 理解成  yyyy-MM-dd 00：00：00 更好理解点
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayStr = format.format(newTime);
        Date today = format.parse(todayStr);
        //昨天 86400000=24*60*60*1000 一天
        if ((today.getTime() - oldTime.getTime()) > 0 && (today.getTime() - oldTime.getTime()) <= 86400000) {
            return 0;
        } else if ((today.getTime() - oldTime.getTime()) <= 0) { //至少是今天
            return -1;
        } else { //至少是前天
            return 1;
        }
    }

    public static boolean number(String number) {
        int b = 0;
        String[] fenge = number.split("");   //以字符间的“空”为分割符
        for (String s : fenge)         //for each 遍历
        {
            if (s.equals("."))     //条件判断 自增加一
            {
                b++;
            }
        }
        if (b > 1) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean numbers(String number) {
        int b = 0;
        String[] fenge = number.split("");   //以字符间的“空”为分割符
        for (String s : fenge)         //for each 遍历
        {
            if (s.equals("-"))     //条件判断 自增加一
            {
                b++;
            }
        }
        if (b > 1) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean isQualified(String price) {
        String pattern = "^[0-9]{1,8}([.][0-9]{1,2})?$";

        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);

        // 现在创建 matcher 对象
        Matcher m = r.matcher(price);

        return m.find();
    }

    public static boolean negative(String number) {

        if (number.length() > 1) {

            String substring = number.substring(number.length() - 1, number.length());

            if ("-".equals(substring)) {
                return true;
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    //根据符号截取符号之前字符串
    public static String getBeforeInterception(String symbol, String text) {
        String specificationsCode = "";
        try {
            specificationsCode = text.substring(0, text.indexOf(symbol));//之前
        } catch (Exception e) {
            specificationsCode = "";
        }
        return specificationsCode;
    }


    //根据符号截取符号之后字符串
    public static String getAfter(String symbol, String text) {
        String specificationsName = "";
        try {
            String specificationsCode = text.substring(0, text.indexOf(symbol));//之前
            specificationsName = text.substring(specificationsCode.length() + 1, text.length());//之后
        } catch (Exception e) {
            specificationsName = text;
        }
        return specificationsName;
    }

    /**
     * 逗号字符串截取首位
     */
    public static String StringList(String string) {

        if (string == null || "".equals(string) || "null".equals(string)) {
            return "";
        }

        List<String> list1 = Arrays.asList(string.split(","));

        return list1.get(0);
    }

    /**
     * 是否是管理方
     */
    public static boolean isManagement(String string) {

        if ("100000000001".equals(string)) {
            return true;
        } else if ("100000000002".equals(string)) {
            return true;
        } else if ("100000000003".equals(string)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否可以开通小云店
     */
    public static boolean isOpen(String string) {

        if ("100000000001".equals(string)) {
            return true;
        } else if ("100000000002".equals(string)) {
            return true;
        } else if ("100000000006".equals(string)) {
            return true;
        } else if ("100000000007".equals(string)) {
            return true;
        } else {
            return false;
        }
    }

    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);

    /**
     * 当API最小版本小于17时使用当前方法生成ID
     * 当API版本大于17时使用 View.generateViewId()方法生成ID
     *
     * @return
     */
    public static int generateViewId() {
        for (; ; ) {
            final int result = sNextGeneratedId.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }

    /*
     * 时间转换
     * */
    public static String getHHMMSSDateDates(long date) {

        if (date < 60) {
            if (date < 10) {
                return date + "秒";
            } else {
                return date + "秒";
            }

        } else if (date > 60 && date < 3600) {
            long m = date / 60;
            long s = date % 60;

            return m + "分" + s + "秒";

        } else {
            long h = date / 3600;
            long m = (date % 3600) / 60;
            long s = (date % 3600) % 60;
            return h + "小时" + m + "分" + s + "秒";
        }

    }


    public static String getFilePathByUri_BELOWAPI11(Context context, String imgPath) {

        Uri uri = Uri.parse((String) imgPath);
        // 以 content:// 开头的，比如 content://media/extenral/images/media/17766
        if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
            String path = null;
            String[] projection = new String[]{MediaStore.Images.Media.DATA};
            Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    if (columnIndex > -1) {
                        path = cursor.getString(columnIndex);
                    }
                }
                cursor.close();
            }
            return path;
        }
        return null;
    }

    public static int isOrgType(String orgTypeId) {
      /*  100000000001 批发市场
        100000000002 农贸市场
        100000000003 商超大卖场
        100000000004 企业配送中心
        100000000005 单位/学校食堂
        100000000006 商会协会
        100000000007 物流运输
        100000000008 仓储服务
        100000000009 屠宰场
        100000000010 市场销售商家
        100000000011 现制现售
        100000000012 街边零售店
        100000000013 商贸公司
        100000000014 餐饮店
        100000000015 中央厨房
        100000000016 生产厂家
        100000000017 小作坊
        100000000018 种植/养殖基地
        100000000019 网络销售
        100000000020 网上订餐
        100000000021 零食摊贩
        100000000022 小吃摊贩
        100000000023 种植/养殖户
        100000000024 商超销售商家*/

        if ("100000000001".equals(orgTypeId) || "100000000002".equals(orgTypeId) || "100000000003".equals(orgTypeId) || "100000000004".equals(orgTypeId) || "100000000006".equals(orgTypeId) ||
                "100000000007".equals(orgTypeId) || "100000000008".equals(orgTypeId) || "100000000009".equals(orgTypeId) || "100000000010".equals(orgTypeId) || "100000000011".equals(orgTypeId) ||
                "100000000012".equals(orgTypeId) || "100000000013".equals(orgTypeId) || "100000000014".equals(orgTypeId) || "100000000015".equals(orgTypeId) || "100000000016".equals(orgTypeId) ||
                "100000000017".equals(orgTypeId) || "100000000018".equals(orgTypeId) || "100000000019".equals(orgTypeId) || "100000000020".equals(orgTypeId)) {
            return 1;
        } else if ("100000000005".equals(orgTypeId)) {
            return 2;
        } else {
            return 3;
        }

    }

    /**
     * 判断字符串是否为URL
     *
     * @param urls 需要判断的String类型url
     * @return true:是URL；false:不是URL
     */
    public static boolean isHttpUrl(String urls) {
        boolean isurl = false;
        String regex = "(((https|http)?://)?([a-z0-9]+[.])|(www.))"
                + "\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)";//设置正则表达式

        Pattern pat = Pattern.compile(regex.trim());//对比
        Matcher mat = pat.matcher(urls.trim());
        isurl = mat.matches();//判断是否匹配
        if (isurl) {
            isurl = true;
        }
        return isurl;
    }

    public static String transformation(String string) {
        string = new StringBuilder(string).reverse().toString();     //先将字符串颠倒顺序
        String str2 = "";
        for (int i = 0; i < string.length(); i++) {
            if (i * 3 + 3 > string.length()) {
                str2 += string.substring(i * 3, string.length());
                break;
            }
            str2 += string.substring(i * 3, i * 3 + 3) + ",";
        }
        if (str2.endsWith(",")) {
            str2 = str2.substring(0, str2.length() - 1);
        }
        String s = new StringBuilder(str2).reverse().toString();
        return s;
    }

    /**
     * @description 校验18位的统一社会信用代码
     * @author CaiCheng
     * @date 2019年11月5日19:17:31
     */
    public static boolean isLicense18(String license) {
        if (license.isEmpty()) {
            return false;
        }
        if (license.length() != 18) {
            return false;
        }

        String regex = "^([159Y]{1})([1239]{1})([0-9ABCDEFGHJKLMNPQRTUWXY]{6})([0-9ABCDEFGHJKLMNPQRTUWXY]{9})([0-90-9ABCDEFGHJKLMNPQRTUWXY])$";
        if (!license.matches(regex)) {
            return false;
        }
        //验证校验位
        String str = "0123456789ABCDEFGHJKLMNPQRTUWXY";
        int[] ws = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28};
        String[] codes = new String[2];
        codes[0] = license.substring(0, license.length() - 1);
        codes[1] = license.substring(license.length() - 1, license.length());
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += str.indexOf(codes[0].charAt(i)) * ws[i];
        }
        int c18 = 31 - (sum % 31);
        if (c18 == 31) {
            c18 = 30;
        }
        if (str.charAt(c18) != codes[1].charAt(0)) {
            return false;
        }
        return true;
    }


}
